package com.puzzlebazar.client.main.model;

import java.util.HashMap;

public class PuzzleListInfoFactory {

	/**
	 * Private constructor for singleton, should register all the 
	 * types of puzzles
	 */
	private PuzzleListInfoFactory() {
		PuzzleListInfoSimple.register( this );
		PuzzleListInfoSearch.register( this );
	}

	private static PuzzleListInfoFactory instance = null;
	public static synchronized PuzzleListInfoFactory getInstance() {
		if( instance == null ) instance = new PuzzleListInfoFactory();
		return instance;
	}
	
	private final HashMap<String, PuzzleListInfoCreator> registeredTypes 
		= new HashMap<String, PuzzleListInfoCreator>();

	/**
	 * Register a new type of puzzle list information
	 * 
	 * @param type The type to register
	 * @param creator The creation function to register
	 */
	public void registerPuzzleListInfo( String type, PuzzleListInfoCreator creator ) {
		registeredTypes.put( type, creator );
	}
	
	/**
	 * Creates a puzzle information structure given a number of parameters. One of these 
	 * parameters should be "TYPE" so that the type of list can be identified
	 * 
	 * @param params The list of parameters for creation, including "Type"
	 * @return
	 * @throws Exception
	 */
	public PuzzleListInfo createPuzzleListInfo( HashMap<String, String> params ) throws Exception {
		String type = PuzzleListInfoImpl.extractInfoType( params );
		if( type == null )
			throw new Exception( "Puzzle list info type not present in parameters" );
		PuzzleListInfoCreator creator = registeredTypes.get( type );
		if( creator == null )
			throw new Exception( "Puzzle list info type \"" + type + "\" not registerd." );
		
		return creator.createPuzzleListInfo( params );		
	}
	
	
	public interface PuzzleListInfoCreator {
		/**
		 * Method responsible for creating a specific puzzle list info structure from
		 * a number of custom parameters 
		 * 
		 * @param params The custom parameters to use to create this
		 * @return The newly created puzzle list information class
		 */
		public PuzzleListInfo createPuzzleListInfo( HashMap<String, String> params );
	}
	
	
}
